
#options(repos = "https://cran.rstudio.com")
library(tidyr)
library(plyr)
library(dplyr)
library(pscl)
library(MASS)
library(stringr)
library(ggplot2)
library(ggeffects)
library(sandwich)
library(ggeffects)
library(margins)
library(ggsci)
library(miceadds)
library(stargazer)
library(clubSandwich)
library(survival) 
library(lmtest)



data_day<-read.csv("data-day.csv")
data_tweet<-read.csv("data-tweets.csv")

data_day_ex<-subset(data_day,!(data_day$user%in%c("GeneralNeva","IsmailOguelleh")))
data_tweet_ex<-subset(data_tweet,!(data_tweet$user%in%c("GeneralNeva","IsmailOguelleh")))




#############################
###     H1: Frequency     ###
#############################


### Main Manuscript ###


# Table 2 #
m_h1a<-glm.nb(formula = tweet_count ~ electoral_democracy+twitter_perc_ave  + age + education+covid_active,data = data_day)
#se<-vcovCR(m_h1a, cluster = data_day$user,type="CR1")
print(coeftest(m_h1a,vcov. = vcovCR(m_h1a, cluster = data_day$user,type="CR1")), digits=2)

m_h1b<-glm.nb(formula = tweet_count ~ electoral_democracy*twitter_perc_ave  + age + education+covid_active,data = data_day)
#se<-vcovCR(m_h1b, cluster = data_day$user,type="CR1")
print(coeftest(m_h1b,vcov. = vcovCR(m_h1b, cluster = data_day$user,type="CR1")), digits=2)

# Figure 2 #
m<- m_h1b
vc<-vcovCR(m,cluster=data_day$user,type="CR1")
a<-seq(quantile(data_day$twitter_perc_ave,0.1, na.rm=TRUE),quantile(data_day$twitter_perc_ave,0.9,na.rm=TRUE),0.1)
marg_fe<-margins(m,at=list(twitter_perc_ave =a),vcov=vc, variables="electoral_democracy")

pdf("Fig_H1.pdf",4.5,4.5)
par(mar=c(4,4,1,1.2))
plot(x=summary(marg_fe)$twitter_perc_ave,y=as.numeric(summary(marg_fe)$AME),type="l",xlab="Twitter Users (% Population)",ylab="AME of Electoral Democracy",cex=0.8, ylim=c(-5,12), main="")
polygon(x=c(summary(marg_fe)$twitter_perc_ave,rev(summary(marg_fe)$twitter_perc_ave)),y=c(summary(marg_fe)$lower,rev(summary(marg_fe)$upper)),density=75,col="lightgray")
lines(x=summary(marg_fe)$twitter_perc_ave,y=as.numeric(summary(marg_fe)$AME))
abline(h=0)
dev.off()


### Appendix ###


#Excluding Ndayishimiye and Guelleh

m_h1a_ex<-glm.nb(formula = tweet_count ~ electoral_democracy+twitter_perc_ave + age + education+covid_active,data = data_day_ex)
print(coeftest(m_h1a_ex,vcov. = vcovCR(m_h1a_ex, cluster = data_day_ex$user,type="CR1")),digits=2)
m_h1b_ex<-glm.nb(formula = tweet_count ~ electoral_democracy*twitter_perc_ave  + age + education+covid_active,data = data_day_ex)
print(coeftest(m_h1b_ex,vcov. = vcovCR(m_h1b_ex, cluster = data_day_ex$user,type="CR1")),digits=2)


# Control for GDP 

data_day$gdp_per_capita_1000<-data_day$gdp_per_capita/1000
data_day$gdp_per_capita_log<-log(data_day$gdp_per_capita)


m_h1a_ed<-glm.nb(formula = tweet_count ~ electoral_democracy+twitter_perc_ave + gdp_per_capita_log + age + education+covid_active,data = data_day)
print(coeftest(m_h1a_ed,vcov. = vcovCR(m_h1a_ed, cluster = data_day$user,type="CR1")),digits=2)

m_h1b_ed<-glm.nb(formula = tweet_count ~ electoral_democracy*twitter_perc_ave + gdp_per_capita_log + age + education+covid_active,data = data_day)
print(coeftest(m_h1b_ed,vcov. = vcovCR(m_h1b_ed, cluster = data_day$user,type="CR1")),digits=2)



           
####################################
###    H2: Elite Interactions    ###
####################################           
        

### Main Manuscript ###    
          
# Table 2 #          
m_h2a<-glm(formula=mentionedUsers_noself ~ electoral_democracy+twitter_perc_ave+age+education+covid_active,family="binomial", data=data_tweet)
se_h2a <-sqrt(diag(vcovCR(m_h2a, cluster = data_tweet$user,type="CR1")))
#coeftest(m_h2a,vcov. = vcovCR(m_h2a, cluster = data_tweet$user,type="CR1"))

m_h2b<-glm(formula=mentionedUsers_noself ~ electoral_democracy*twitter_perc_ave+age+education+covid_active,family="binomial", data=data_tweet)
se_h2b<-sqrt(diag(vcovCR(m_h2b, cluster = data_tweet$user,type="CR1")))
#coeftest(m_h2b,vcov. = vcovCR(m_h2b, cluster = data_tweet$user,type="CR1"))

stargazer(m_h2a,m_h2b,
          se=list(se_h2a,se_h2b),
          label="tab:H2",
          title="Relationship between Democracy and Inclusivity",
          dep.var.labels.include = FALSE,
          model.names=FALSE,
          model.numbers=FALSE,
       covariate.labels=c("Electoral Dem. Score","Twitter Users (Perc 18+ Pop)","Age","Education","Covid Active","Elec. Dem X Twitter Users","Constant"),
          add.lines = c(list(c("Unit of Analysis","Tweet","Tweet")))
          )    
# Figure 2 #
m<- m_h2b
vc<-vcovCR(m,cluster=data_tweet$user,type="CR1")
a<-seq(quantile(data_tweet$twitter_perc_ave,0.1, na.rm=TRUE),quantile(data_tweet$twitter_perc_ave,0.9,na.rm=TRUE),0.1)
marg_fe<-margins(m,at=list(twitter_perc_ave=a),vcov=vc, variables="electoral_democracy")

pdf("Fig_H2.pdf",4.5,4.5)
par(mar=c(4,4,1,1.2))
plot(x=summary(marg_fe)$twitter_perc_ave,y=as.numeric(summary(marg_fe)$AME),type="l",xlab="Twitter Users (% 18+ Population)",ylab="AME of Electoral Democracy",cex=0.8, ylim=c(-2,0.5), main="")
polygon(x=c(summary(marg_fe)$twitter_perc_ave,rev(summary(marg_fe)$twitter_perc_ave)),y=c(summary(marg_fe)$lower,rev(summary(marg_fe)$upper)),density=75,col="lightgray")
lines(x=summary(marg_fe)$twitter_perc_ave,y=as.numeric(summary(marg_fe)$AME))
abline(h=0)
dev.off()


### Appendix ###

#Excluding Ndayishimiye and Guelleh         
  m_h2a_ex<-glm(formula=mentionedUsers_noself ~ electoral_democracy+twitter_perc_ave+age+education+covid_active,family="binomial", data=data_tweet_ex)
se_h2a_ex <-sqrt(diag(vcovCR(m_h2a_ex, cluster = data_tweet_ex$user,type="CR1")))

m_h2b_ex<-glm(formula=mentionedUsers_noself ~ electoral_democracy*twitter_perc_ave+age+education+covid_active,family="binomial", data=data_tweet_ex)
se_h2b_ex<-sqrt(diag(vcovCR(m_h2b_ex, cluster = data_tweet_ex$user,type="CR1")))

stargazer(m_h2a_ex,m_h2b_ex,
          se=list(se_h2a_ex,se_h2b_ex),
          label="tab:H2",
          title="Relationship between Democracy and Inclusivity",
          dep.var.labels.include = FALSE,
          model.names=FALSE,
          model.numbers=FALSE,
       covariate.labels=c("Electoral Dem. Score","Twitter Users (% 18+ Pop)","Age","Education","Covid Active","Elec. Dem X Twitter Users","Constant"),
          add.lines = c(list(c("Unit of Analysis","Tweet","Tweet")))
          )
            
        
# Control for Economic Development
data_tweet$gdp_per_capita_1000<-data_tweet$gdp_per_capita/1000
data_tweet$gdp_per_capita_log<-log(data_tweet$gdp_per_capita)

m_h2a_ed<-glm(formula=mentionedUsers_noself ~ electoral_democracy+twitter_perc_ave+ gdp_per_capita_log+age+education+covid_active,family="binomial", data=data_tweet)
se_h2a_ed <-sqrt(diag(vcovCR(m_h2a_ed, cluster = data_tweet$user,type="CR1")))

m_h2b_ed<-glm(formula=mentionedUsers_noself ~ electoral_democracy*twitter_perc_ave+gdp_per_capita_log+age+education+covid_active,family="binomial", data=data_tweet)
se_h2b_ed<-sqrt(diag(vcovCR(m_h2b_ed, cluster = data_tweet$user,type="CR1")))
         
stargazer(m_h2a_ed,m_h2b_ed,
          se=list(se_h2a_ed,se_h2b_ed),
          label="tab:H2",
          title="Relationship between Democracy and Inclusivity, Controlling for Economic Development",
          dep.var.labels.include = FALSE,
          model.names=FALSE,
          model.numbers=FALSE,
       covariate.labels=c("Electoral Dem. Score","Twitter Users (% 18+ Pop)","Log GDP Per Capita","Age","Education","Covid Active","Elec. Dem X Twitter Users","Constant"),
          add.lines = c(list(c("Unit of Analysis","Tweet","Tweet")))
          )    
  
     


#############################
###     H3: Sentiment     ###
#############################  

### Main Manuscript ###

# Table 2 #

#start here. 

m_h3a<- lm(bert_score~electoral_democracy+twitter_perc_ave+ age + education+lang+covid_active,data = data_tweet)
se_h3a <-sqrt(diag(vcovCR(m_h3a, cluster = data_tweet$user,type="CR1")))
#coeftest(m_h3a,vcov. = vcovCR(m_h3, cluster = data_tweet$user,type="CR1"))

m_h3b<- lm(bert_score~electoral_democracy*twitter_perc_ave+ age + education+lang+covid_active,data = data_tweet)
se_h3b <-sqrt(diag(vcovCR(m_h3b, cluster = data_tweet$user,type="CR1")))
#coeftest(m_h3a,vcov. = vcovCR(m_h3, cluster = data_tweet$user,type="CR1"))


stargazer(m_h3a,m_h3b,
          se=list(se_h3a,se_h3b),
          title="Effect of Democracy on Tweet Sentiment",
          dep.var.labels.include = FALSE,
          model.names=FALSE,
          model.numbers=FALSE,
      #covariate.labels=c("Electoral Dem. Score","Covid Active","Twitter Users (% 18+ Pop)","Age","Education","French","Elec. Dem X Twitter Users","Elec. Dem X Covid Active","Constant"),
          add.lines = c(list(c("Unit of Analysis","Tweet","Tweet")))
          )


# Figure for H3a #

m<- m_h3b
vc<-vcovCR(m, cluster = data_tweet$user,type="CR1")
a<-seq(quantile(data_tweet$twitter_perc_ave,0.1, na.rm=TRUE),quantile(data_tweet$twitter_perc_ave,0.9,na.rm=TRUE),0.1)
marg_fe<-margins(m,at=list(twitter_perc_ave=a),vcov=vc, variables="electoral_democracy")


pdf("Fig_H3.pdf",4.5,4.5)
par(mar=c(4,4,1,1.2))
plot(x=summary(marg_fe)$twitter_perc_ave,y=as.numeric(summary(marg_fe)$AME),type="l",xlab="Twitter Users  (% Pop)",ylab="Average Marginal Effect of Electoral Democracy",cex=0.8, main="",ylim=c(-2,0.5))
polygon(x=c(summary(marg_fe)$twitter_perc_ave,rev(summary(marg_fe)$twitter_perc_ave)),y=c(summary(marg_fe)$lower,rev(summary(marg_fe)$upper)),density=75,col="lightgray")
lines(x=summary(marg_fe)$twitter_perc_ave,y=as.numeric(summary(marg_fe)$AME))
abline(h=0)
dev.off()




### Appendix ###

# Table #

#Excluding Ndayishimiye and Guelleh    
m_h3a_ex<- lm(bert_score~electoral_democracy+covid_active+twitter_perc_ave+ age + education+lang,data = data_tweet_ex)
se_h3a_ex <-sqrt(diag(vcovCR(m_h3a_ex, cluster = data_tweet_ex$user,type="CR1")))

m_h3b_ex<- lm(bert_score~electoral_democracy*twitter_perc_ave+covid_active+ age + education+lang,data = data_tweet_ex)
se_h3b_ex <-sqrt(diag(vcovCR(m_h3b_ex, cluster = data_tweet_ex$user,type="CR1")))


stargazer(m_h3a_ex,m_h3b_ex,
          se=list(se_h3a_ex,se_h3b_ex),
          title="Effect of Democracy on Tweet Sentiment",
          dep.var.labels.include = FALSE,
          model.names=FALSE,
          model.numbers=FALSE,
      covariate.labels=c("Electoral Dem. Score","Covid Active","Twitter Users (% 18+ Pop)","Age","Education","French","Elec. Dem X Twitter Users","Elec. Dem X Covid Active","Constant"),
          add.lines = c(list(c("Unit of Analysis","Tweet","Tweet","Tweet")))
          )

#Controlling for ED
data_tweet$gdp_per_capita_1000<-data_tweet$gdp_per_capita/1000
data_tweet$gdp_per_capita_log<-log(data_tweet$gdp_per_capita)


m_h3a_ed<- lm(bert_score~electoral_democracy+twitter_perc_ave+gdp_per_capita_log+ age + education+lang+covid_active,data = data_tweet)
se_h3a_ed <-sqrt(diag(vcovCR(m_h3a_ed, cluster = data_tweet$user,type="CR1")))

m_h3b_ed<- lm(bert_score~electoral_democracy*twitter_perc_ave+gdp_per_capita_log+ age + education+lang+covid_active,data = data_tweet)
se_h3b_ed <-sqrt(diag(vcovCR(m_h3b_ed, cluster = data_tweet$user,type="CR1")))


stargazer(m_h3a_ed,m_h3b_ed,
          se=list(se_h3a_ed,se_h3b_ed),
          title="Effect of Democracy on Tweet Sentiment",
          dep.var.labels.include = FALSE,
          model.names=FALSE,
          model.numbers=FALSE,
      covariate.labels=c("Electoral Dem. Score","Twitter Users (% 18+ Pop)","Log GDP per capita","Age","Education","French","Covid Active","Elec. Dem X Twitter Users","Constant"),
          add.lines = c(list(c("Unit of Analysis","Tweet","Tweet")))
          )



#############################
###     H4: Elections     ###
#############################  


### Main Manuscript ###

# Table 3 # 

m_h4a<-glm.nb(tweet_count~ pre_90 + post_90 +  electoral_democracy +twitter_perc_ave+covid_active + age + education,data=data_day)
coeftest(m_h4a,vcov. = vcovCR(m_h4a, cluster = data_day$user,type="CR1"))

m_h4b<-glm.nb(tweet_count~ electoral_democracy*pre_90 + electoral_democracy*post_90  +twitter_perc_ave +covid_active + age + education,data=data_day)
coeftest(m_h4b,vcov. = vcovCR(m_h4b, cluster = data_day$user,type="CR1"))

m_h4c <- lm(bert_score~pre_90  + post_90 + electoral_democracy +twitter_perc_ave+ covid_active+ age + education+lang,data = data_tweet)
se_h4c <-sqrt(diag(vcovCR(m_h4c, cluster = data_tweet$user,type="CR1")))

m_h4d <- lm(bert_score~electoral_democracy*pre_90 + electoral_democracy*post_90  +twitter_perc_ave+covid_active+ age + education+lang,data = data_tweet)
se_h4d <-sqrt(diag(vcovCR(m_h4d, cluster = data_tweet$user,type="CR1")))

stargazer(m_h4c,m_h4d,
          se=list(se_h4c,se_h4d),
          label="tab:H3",
          title="Relationship Between Elections and Twitter Usage",
          dep.var.labels.include = FALSE,
          model.names=FALSE,
          model.numbers=FALSE,
         covariate.labels=c("Pre-Electoral","Post Electoral","Electoral Dem. Score","Twitter Users (% 18+ Pop)","Covid Active","Age","Education","French","Elec. Dem. X Pre-Electoral","Elec. Dem. X Post-Electoral","Constant"),
          add.lines = c(list(c("Unit of Analysis","Tweet","Tweet")))
          )




# Figure 3 #

#Panel A
m<- m_h4b
vc<-vcovCR(m,cluster=data_day$user,type="CR1")
marg_pre<-margins(m,at=list(electoral_democracy=c(seq(min(data_day$electoral_democracy),max(data_day$electoral_democracy),0.01)),post_90=0),vcov=vc, variables="pre_90")
#marg_post<-margins(m,at=list(electoral_democracy=c(seq(min(data_day$electoral_democracy),max(data_day$electoral_democracy),0.01)),pre_90=0),vcov=vc, variables="post_90")

pdf("Fig_H4a.pdf",4.5,4.5)
par(mar=c(4,4,1,1.2))
plot(x=summary(marg_pre)$electoral_democracy,y=summary(marg_pre)$AME,type="l",ylim=c(-1,2),ylab="AME (Baseline = Non-Electoral Period)",xlab="Electoral Democracy Score",main="")
#lines(x=summary(marg_post)$electoral_democracy,y=summary(marg_post)$AME)
#polygon(x=c(summary(marg_post)$electoral_democracy,rev(summary(marg_post)$electoral_democracy)),y=c(summary(marg_post)$lower,rev(summary(marg_post)$upper)),density=75,col="lightgray")
polygon(x=c(summary(marg_pre)$electoral_democracy,rev(summary(marg_pre)$electoral_democracy)),y=c(summary(marg_pre)$lower,rev(summary(marg_pre)$upper)),density=75,col="darkgray")
#lines(x=summary(marg_post)$electoral_democracy,y=summary(marg_post)$AME)
lines(x=summary(marg_pre)$electoral_democracy,y=summary(marg_pre)$AME)
#legend(x=0.2,y=-1, legend=c("Pre-Electoral Period","Post-Electoral Period"),pch=20,col=c("darkgray","lightgray"))
abline(h=0)
dev.off()


#Panel B
m <- m_h4d
vc<-vcovCR(m,cluster= data_tweet$user,type="CR1")
marg_pre<-margins(m,at=list(electoral_democracy=c(seq(min(data_tweet$electoral_democracy),max(data_tweet$electoral_democracy),0.01)),post_90=0),vcov=vc, variables="pre_90")
#marg_post<-margins(m,at=list(electoral_democracy=c(seq(min(data_tweet$electoral_democracy),max(data_tweet$electoral_democracy),0.01)),pre_90=0),vcov=vc, variables="post_90")


pdf("Fig_H4b.pdf",4.5,4.5)
par(mar=c(4,4,1,1.2))
plot(x=summary(marg_pre)$electoral_democracy,y=summary(marg_pre)$AME,type="l",ylim=c(-0.1,0.3),ylab="AME (Baseline = Non-Electoral Period",xlab="Electoral Democracy Score",main="")
#lines(x=summary(marg_post)$electoral_democracy,y=summary(marg_post)$AME)
#polygon(x=c(summary(marg_post)$electoral_democracy,rev(summary(marg_post)$electoral_democracy)),y=c(summary(marg_post)$lower,rev(summary(marg_post)$upper)),density=75,col="lightgray")
polygon(x=c(summary(marg_pre)$electoral_democracy,rev(summary(marg_pre)$electoral_democracy)),y=c(summary(marg_pre)$lower,rev(summary(marg_pre)$upper)),density=75,col="darkgray")
#legend(x=0.25,y=-0.03, legend=c("Pre-Electoral Period","Post-Electoral Period"),pch=20,col=c("darkgray","lightgray"))
abline(h=0)
lines(x=summary(marg_pre)$electoral_democracy,y=summary(marg_pre)$AME)
#lines(x=summary(marg_post)$electoral_democracy,y=summary(marg_post)$AME)
dev.off()


### Appendix  ###


#Excluding Ndayishimiye and Guelleh    

m_h4a_ex<-glm.nb(tweet_count~ electoral_democracy*pre_90 + electoral_democracy*post_90 +twitter_perc_ave +covid_active + age + education,data=data_day_ex)
coeftest(m_h4a_ex,vcov. = vcovCR(m_h4a_ex, cluster = data_day_ex$user,type="CR1"))

m_h4c_ex <- lm(bert_score~electoral_democracy*pre_90 + electoral_democracy*post_90 +twitter_perc_ave+covid_active+ age + education+lang,data = data_tweet_ex)
se_h4c_ex <-sqrt(diag(vcovCR(m_h4c_ex, cluster = data_tweet_ex$user,type="CR1")))

stargazer(m_h4c_ex,
          se=list(se_h4c_ex),
          label="Tab_H4_Ex",
          title="Elections and Twitter Usage",
          dep.var.labels.include = FALSE,
          model.names=FALSE,
          model.numbers=FALSE,
          covariate.labels=c("Electoral Dem. Score","Pre-Electoral","Post Electoral","Twitter Users (% Pop)","Covid Active","Age","Education","French","Elec. Dem. X Pre-Electoral","Elec. Dem. X Post-Electoral","Constant"),
          add.lines = c(list(c("Unit of Analysis","Tweet")))
          )

# Controlling for Economic Development

data_tweet$gdp_per_capita_1000<-data_tweet$gdp_per_capita/1000
data_tweet$gdp_per_capita_log<-log(data_tweet$gdp_per_capita)

data_day$gdp_per_capita_1000<-data_day$gdp_per_capita/1000
data_day$gdp_per_capita_log<-log(data_day$gdp_per_capita)


m_h4a_ed<-glm.nb(tweet_count~ electoral_democracy*pre_90 + electoral_democracy*post_90 +twitter_perc_ave + gdp_per_capita_log+covid_active + age + education,data=data_day)
coeftest(m_h4a_ed,vcov. = vcovCR(m_h4a_ed, cluster = data_day$user,type="CR1"))

m_h4c_ed <- lm(bert_score~electoral_democracy*pre_90 + electoral_democracy*post_90 +twitter_perc_ave+gdp_per_capita_log + covid_active+ age + education+lang,data = data_tweet)
se_h4c_ed <-sqrt(diag(vcovCR(m_h4c_ed, cluster = data_tweet$user,type="CR1")))

stargazer(m_h4c_ed,
          se=list(se_h4c_ed),
          label="Tab_H4_Ed",
          title="Elections and Twitter Usage, controlling for Economic Development",
          dep.var.labels.include = FALSE,
          model.names=FALSE,
          model.numbers=FALSE,
          covariate.labels=c("Electoral Dem. Score","Pre-Electoral","Post Electoral","Twitter Users (% 18+ Pop)","Log GDP per capita","Covid Active","Age","Education","French","Elec. Dem. X Pre-Electoral","Elec. Dem. X Post-Electoral","Constant"),
          add.lines = c(list(c("Unit of Analysis","Tweet")))
          )
          
 
 #interaction models
 
 
data_tweet_high<-subset(data_tweet,data_tweet$twitter_perc_ave>=median(data_tweet$twitter_perc_ave))
data_tweet_low<-subset(data_tweet,data_tweet$twitter_perc_ave<median(data_tweet$twitter_perc_ave))

data_day_high<-subset(data_day,data_day$twitter_perc_ave>=median(data_day$twitter_perc_ave))
data_day_low<-subset(data_day,data_day$twitter_perc_ave<median(data_day$twitter_perc_ave))


# Table #

m_h4a<-glm.nb(tweet_count~ pre_90*twitter_perc_ave  + post_90*twitter_perc_ave+ electoral_democracy +covid_active + age + education,data=data_day)
coeftest(m_h4a,vcov. = vcovCR(m_h4a, cluster = data_day$user,type="CR1"))

m_h4a_high<-glm.nb(tweet_count~ electoral_democracy*pre_90 + electoral_democracy*post_90+twitter_perc_ave +covid_active + age + education,data=data_day_high)
coeftest(m_h4a_high,vcov. = vcovCR(m_h4a_high, cluster = data_day_high$user,type="CR1"))

m_h4a_low<-glm.nb(tweet_count~ electoral_democracy*pre_90 + electoral_democracy*post_90+twitter_perc_ave +covid_active + age + education,data=data_day_low)
coeftest(m_h4a_low,vcov. = vcovCR(m_h4a_low, cluster = data_day_low$user,type="CR1"))

m_h4b<- lm(bert_score~pre_90*twitter_perc_ave  + post_90*twitter_perc_ave+ electoral_democracy+covid_active+ age + education+lang,data = data_tweet)
se_h4b <-sqrt(diag(vcovCR(m_h4b, cluster = data_tweet$user,type="CR1")))

m_h4b_high <- lm(bert_score~electoral_democracy*pre_90 + electoral_democracy*post_90+twitter_perc_ave+covid_active+ age + education+lang,data = data_tweet_high)
se_h4b_high <-sqrt(diag(vcovCR(m_h4b_high, cluster = data_tweet_high$user,type="CR1")))

m_h4b_low <- lm(bert_score~electoral_democracy*pre_90 + electoral_democracy*post_90+twitter_perc_ave+covid_active+ age + education+lang,data = data_tweet_low)
se_h4b_low <-sqrt(diag(vcovCR(m_h4b_low, cluster = data_tweet_low$user,type="CR1")))

stargazer(m_h4b,m_h4b_high,m_h4b_low,
          se=list(se_h4b,se_h4b_high,se_h4b_low),
          title="Elections and Positivity",
          dep.var.labels.include = FALSE,
          model.names=FALSE,
          model.numbers=FALSE,
     
     #     covariate.labels=c("Pre-Electoral","Twitter Users (% Pop)","Post Electoral","Electoral Democracy","Covid Active","Age","Education","French","Twitter Users X Pre-Electoral","Twitter Users X Post-Electoral","Elec. Dem. X Pre-Electoral","Elec. Dem. X Post-Electoral","Constant"),
          add.lines = c(list(c("Unit of Analysis","Tweet","Tweet","Tweet")))
          )

 
 
 